import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RMISecurityManager;

import net.jini.lookup.entry.Name;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.entry.Entry;
import net.jini.core.lease.Lease;
import net.jini.core.lookup.ServiceTemplate;


public class CopyCenter extends UnicastRemoteObject implements SynchronizeService {

	public MassStorageDevice getDiscDevice() {
		try {
			LookupLocator lookup = new LookupLocator ("jini://localhost");
	
			ServiceRegistrar registrar  = lookup.getRegistrar();
			ServiceTemplate template = new ServiceTemplate (null, new Class[] {MassStorageDevice.class}, null);
				
			return (MassStorageDevice) registrar.lookup (template);
		} catch (Exception e) {
			System.out.println("No hard drives found, process aborted");		
			e.printStackTrace();
			return null;
		} 
	}
	
	public Camera getCameraDevice() {
		try {
			LookupLocator lookup = new LookupLocator ("jini://localhost");
	
			ServiceRegistrar registrar  = lookup.getRegistrar();
			ServiceTemplate template = new ServiceTemplate (null, new Class[] {Camera.class}, null);
				
			return (Camera) registrar.lookup (template);
		} catch (Exception e) {
			System.out.println("No cameras found, process aborted");		
			e.printStackTrace();
			return null;
		} 
	}
	
	public PrintService getPrinterDevice() {
		try {
			LookupLocator lookup = new LookupLocator ("jini://localhost");
	
			ServiceRegistrar registrar  = lookup.getRegistrar();
			ServiceTemplate template = new ServiceTemplate (null, new Class[] {PrintService.class}, null);
				
			return (PrintService) registrar.lookup (template);
		} catch (Exception e) {
			System.out.println("No printers found, process aborted");		
			e.printStackTrace();
			return null;
		} 
	}
	
	public void 	download() throws RemoteException {
		System.out.println("Ivoked download all photos process");		
		MassStorageDevice hdd = getDiscDevice();
		Camera cam = getCameraDevice();
		
		if (hdd == null || cam == null) {
			System.out.println("Unable to download photos");
			return;
		}
		
		for (String name : cam.getImageNames()) {
			System.out.println("Downloading photo: " + name);			
			byte[] bitmap = cam.getImage(name);
			hdd.writeFile(name, bitmap);
		}
		
		System.out.println("Done");		
	}
	
	public void 	print() throws RemoteException {
		System.out.println("Ivoked download all photos process");		
		PrintService printer = getPrinterDevice();
		Camera cam = getCameraDevice();
			
		if (cam == null || printer == null) {
			System.out.println("Unable to print photos");		
			return;
		}
		 
		for (String name : cam.getImageNames()) {
			System.out.println("Printing photo: " + name);			
			byte[] bitmap = cam.getImage(name);
			printer.printBitmap(bitmap);
		}
		
		System.out.println("Done");		
		
	}
	
	public CopyCenter() throws RemoteException {
		super ();
	}
	
	public static void main (String[] args) {
		try {
			System.setSecurityManager (new RMISecurityManager ());
			
			ServiceItem serviceItem = new ServiceItem(null, new CopyCenter(), null);

			LookupLocator lookup = new LookupLocator ("jini://localhost");
			ServiceRegistrar registrar  = lookup.getRegistrar();
			
			registrar.register(serviceItem, Lease.FOREVER);

			System.out.println("Copy Center online");

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

} 